<?php

namespace app\common\model;

use think\Model;
use think\Db;

class Cart extends Model
{
    public function shop()
    {
        return $this->belongsTo('User', 'shop_id', 'uid')
			->field('uid,name,tel');
    }

    public function goods()
    {
        return $this->belongsTo('Goods', 'goods_id', 'id')
			->field('id,goods_name,goods_pic');
    }

    public function sku()
    {
        return $this->belongsTo('GoodsSku', 'sku_id', 'sku_id')
			->field('sku_id,sku_name,stock,realprice,oldprice');
    }

    /**
     * 返回购物车列表
     * 
     * @param  [type] $map [description]
     * @return [type]      [description]
     */
    public function getList($map)
    {
        $list = $this->with('shop,goods,sku')
            ->where($map)
            ->field('id,sku_id,shop_id,goods_id,goods_num')
            ->select();

        return $list;
    }

    /**
     * 添加
     * 
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function addInfo($data)
    {
        $cart = $this->where(['uid'=>$data['uid'], 'sku_id'=>$data['sku_id']])->find();
        $num = isset($data['num']) ? $data['num'] : 1;
        if ($cart) {
            $rst = $this->where('id', $cart['id'])->setInc('goods_num', $num);
            return msg_code('已添加', $rst);
        }

        $skuInfo = model('GoodsSku')->getInfo($data['sku_id']);

        // if ($skuInfo->stock <= 0) {
        //     return msg_code('库存为0，暂不能购买！');
        // }
        if ($skuInfo->goods->shop_id == $data['uid']) {
            return msg_code('不支持购买自己的商品');
        }

        $data['shop_id'] = $skuInfo->goods->shop_id;
        $data['goods_num'] = $num;
      
        $rst = $this->save($data);
        return msg_code('添加成功', $rst);
    }

    // 获取某一个购物车记录id，的订单信息
    public function getInfo($id)
    {
        $return = [];
        $skuInfo = $this->with('sku,goods')->where('id', $id)->find();

        $return['money'] = $skuInfo->goods_num * $skuInfo->sku->realprice;
        $return['goods_info'] = [
            'goods_id' => $skuInfo->goods->id,
            'goods_name' => $skuInfo->goods->goods_name,
            'goods_pic' => $skuInfo->goods->goods_pic,
            'sku_id' => $skuInfo->sku->sku_id,
            'sku_name' => $skuInfo->sku->sku_name,
            'sku_price' => $skuInfo->sku->realprice,
            'goods_num' => $skuInfo->goods_num,
        ];
       
        return $return;
    }

    /**
     * 修改购物车
     *
     * @param [type] 购物车记录id
     * @param [type] 变更数目
     * @return void
     */
    public function editInfo($id, $num)
    {
        $goods_num = $this->where('id', $id)->value('goods_num');
        if (empty($goods_num)) {
            return msg_code('此商品已移除');
        }
        if ($goods_num + $num == 0) {
            $rst = $this->where('id', $id)->delete();
        } else {
            $rst = $this->where('id', $id)->setInc('goods_num', $num);
        }

        return msg_code('操作完成',$rst);
    }

    /**
     * 清空购物车
     * 
     * @param  [type] $ids [description]
     * @return [type]      [description]
     */
    public function clear($ids)
    {
        $ids = (array) $ids;
        $rst = $this->where(['id' => ['in',$ids]])->delete();
        return msg_code('操作成功', $rst);
    }


}
